package com.sokal.easycare.modules.order.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.service.CrudService;
import com.sokal.easycare.enums.OrderEnums;
import com.sokal.easycare.modules.order.dao.UserOrderDao;
import com.sokal.easycare.modules.order.dao.item.UserOrderItemDao;
import com.sokal.easycare.modules.order.dao.rec.OmOrderStateRecDao;
import com.sokal.easycare.modules.order.dao.track.OmOrderTrackDao;
import com.sokal.easycare.modules.order.entity.UserOrder;
import com.sokal.easycare.modules.order.entity.item.UserOrderItem;
import com.sokal.easycare.modules.order.entity.rec.OmOrderStateRec;
import com.sokal.easycare.modules.order.entity.track.OmOrderTrack;
import com.sokal.easycare.modules.order.pojo.OrderPojo;
import com.sokal.easycare.modules.product.dao.product.ProProductDao;
import com.sokal.easycare.modules.service.dao.SerServiceDao;
import com.sokal.easycare.modules.sys.dao.UserAddrDao;
import com.sokal.easycare.modules.sys.entity.MainUser;
import com.sokal.easycare.modules.sys.utils.UserUtils;

/**
 * 订单相关Service
 * 
 * @author nibo
 * @version 2017-04-01
 */
@Service
@Transactional(readOnly = true)
public class UserOrderService extends CrudService<UserOrderDao, UserOrder> {
	@Autowired
	OmOrderTrackDao omOrderTrackDao;
	@Autowired
	OmOrderStateRecDao omOrderStateRecDao;
	@Autowired
	UserOrderItemDao orderItemDao;
	@Autowired
	private ProProductDao proProductDao;
	@Autowired
	private SerServiceDao serServiceDao;
	@Autowired
	private UserAddrDao userAddrDao;
	@Autowired
	private OmOrderTrackDao orderTrackDao;
	@Autowired
	private UserOrderDao userOrderDao;

	@Transactional(readOnly = false)
	public void updateOrder(UserOrder userOrder) {
		userOrderDao.update(userOrder);
	}

	public OrderPojo getOrderPojo(String id) {
		OrderPojo orderPojo = new OrderPojo();
		UserOrder userOrder = super.get(id);
		orderPojo.setUserOrder(userOrder);
		fillorderuser(userOrder);
		fillorderUserAddr(userOrder);
		fillomOrderTrack(orderPojo);
		fillorderitems(userOrder);
		return orderPojo;
	}

	public List<UserOrder> findList(UserOrder userOrder) {
		List<UserOrder> list = super.findList(userOrder);
		fillorderuser(list);
		return list;
	}

	public Page<UserOrder> findPage(Page<UserOrder> page, UserOrder userOrder) {
		page = super.findPage(page, userOrder);
		page.getList();
		List<UserOrder> list = page.getList();
		fillorderuser(list);
		for (UserOrder userOrder2 : list) {
			List<OmOrderTrack> orderTracks = omOrderTrackDao.findByOrderCode(userOrder2.getOrderCode());
			for (OmOrderTrack omOrderTrack : orderTracks) {
				// omOrderTrack.getType()
				if (OrderEnums.WorkOrderTypeEnum.sale.value().equals(omOrderTrack.getType())
						|| OrderEnums.WorkOrderTypeEnum.service.value().equals(omOrderTrack.getType())) {
					userOrder2.setOmOrderTrack(omOrderTrack);
				}
			}
		}
		page.setList(list);
		return page;
	}

	@Transactional(readOnly = false)
	public void save(UserOrder userOrder) {
		super.save(userOrder);
	}

	@Transactional(readOnly = false)
	public void delete(UserOrder userOrder) {
		super.delete(userOrder);
	}

	/**
	 * 创建订单跟踪人信息
	 * 
	 * @param trackStaffName
	 *            登录名(电话号码)
	 * @param orderCode
	 *            订单号
	 * @param type
	 *            类型
	 * @param status
	 *            状态
	 * @return
	 */

	@Transactional(readOnly = false)
	public void crearOrderTrack(String trackStaffName, String orderCode, String orderItemCode, String type,
			String status, String trackComments) {
		String trackStaffid = UserUtils.getByLoginName(trackStaffName).getId();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String id = format.format(new Date());
		OmOrderTrack omOrderTrack = new OmOrderTrack(orderCode + "sale");

		omOrderTrack.setTrackStaffId(trackStaffid);
		omOrderTrack.setCreateDate(new Date());
		omOrderTrack.setType(type);
		omOrderTrack.setOrderItemCode(orderItemCode);
		omOrderTrack.setTrackStaffName(trackStaffName);
		// omOrderTrack.setTrackDate(new Date());
		omOrderTrack.setUpdateDate(new Date());
		omOrderTrack.setOrderCode(orderCode);
		omOrderTrack.setStatus(status);
		omOrderTrack.setTrackComments(trackComments);
		orderTrackDao.insert(omOrderTrack);
		createOrderRec(orderCode, "", "添加工单:" + omOrderTrack.toString());
	}

	/**
	 * 填装user
	 * 
	 * @param userOrders
	 */
	public void fillorderuser(List<UserOrder> userOrders) {
		for (UserOrder userOrder : userOrders) {
			fillorderuser(userOrder);
		}
	}

	public void fillorderuser(UserOrder userOrder) {

		userOrder.setUser(UserUtils.get(userOrder.getUserId()));
	}

	public void fillorderUserAddr(UserOrder userOrder) {
		userOrder.setUserAddr(userAddrDao.get(userOrder.getAddressId()));
	}

	public void fillorderitems(UserOrder userOrder) {
		UserOrderItem item = new UserOrderItem();
		item.setOrderCode(userOrder.getOrderCode());
		userOrder.setUserOrderItems(orderItemDao.findList(item));
		fillofferPack(userOrder.getUserOrderItems());
	}

	private void fillofferPack(List<UserOrderItem> userOrderItems) {
		for (UserOrderItem userOrderItem : userOrderItems) {
			fillofferPack(userOrderItem);
		}

	}

	private void fillofferPack(UserOrderItem userOrderItem) {
		// 实物
		if ("1".equals(userOrderItem.getType())) {
			// ProProduct product= proProductDao.get(userOrderItem.getItemId());

			userOrderItem.setOfferPackData(proProductDao.get(userOrderItem.getItemId()));

		} else if ("2".equals(userOrderItem.getType())) {
			// 服务
			userOrderItem.setOfferPackData(serServiceDao.get(userOrderItem.getItemId()));
		} else {

		}

	}

	public void fillomOrderTrack(OrderPojo orderPojo) {
		orderPojo.setOmOrderTrackList(omOrderTrackDao.findByOrderCode(orderPojo.getUserOrder().getOrderCode()));

		for (OmOrderTrack omOrderTrack : orderPojo.getOmOrderTrackList()) {
			omOrderTrack.setTrackStaffUser(UserUtils.get(omOrderTrack.getTrackStaffId()));
			omOrderTrack.setSupervisorUser(UserUtils.get(omOrderTrack.getSupervisorId()));
			if (OrderEnums.WorkOrderTypeEnum.sale.value().equals(omOrderTrack.getType())|| OrderEnums.WorkOrderTypeEnum.service.value().equals(omOrderTrack.getType())) {
				orderPojo.getUserOrder().setOmOrderTrack(omOrderTrack);
			}
		}
	}

	@Transactional(readOnly = false)
	public void updateOmOrderTrack(String orderCode, String trackStaffName) {
		// OmOrderTrack omOrderTrack =
		// omOrderTrackDao.findByOrderCode(orderCode);
		// if (omOrderTrack == null) {
		// omOrderTrack = new OmOrderTrack(System.currentTimeMillis() + "");
		// omOrderTrack.setCreateDate(new Date());
		// omOrderTrack.setTrackDate(new Date());
		// omOrderTrack.setOrderCode(orderCode);
		// omOrderTrack.setTrackStaffId(UserUtils.getByLoginName(trackStaffName).getId());
		// omOrderTrack.setTrackStaffName(trackStaffName);
		// omOrderTrackDao.insert(omOrderTrack);
		// createOrderRec(orderCode, "", "添加订单跟踪:"+trackStaffName);
		// } else {
		// omOrderTrack.setTrackStaffId(UserUtils.getByLoginName(trackStaffName).getId());
		// omOrderTrack.setTrackStaffName(trackStaffName);
		// if (StringUtils.isEmpty(trackStaffName)) {
		// omOrderTrackDao.delete(omOrderTrack);
		// createOrderRec(orderCode, "", "删除订单跟踪:"+trackStaffName);
		// } else {
		// omOrderTrackDao.update(omOrderTrack);
		// createOrderRec(orderCode, "", "更新订单跟踪:"+trackStaffName);
		// }
		// }
		// userOrder.setOmOrderTrack(omOrderTrackDao.findByOrderCode(userOrder.getOrderCode()));
	}

	/**
	 * 创建订单流水
	 * 
	 * @param orderCode
	 *            订单号
	 * @param state
	 *            状态
	 * @param msg
	 *            备注
	 */
	private void createOrderRec(String orderCode, String state, String msg) {
		OmOrderStateRec orderStateRec = new OmOrderStateRec();
		orderStateRec.setOrderCode(orderCode);
		orderStateRec.setUserID(UserUtils.getUser().getId());
		orderStateRec.setState(state);
		orderStateRec.setCreateDate(new Date());
		orderStateRec.setStateDate(new Date());
		orderStateRec.setNotifyMsg(msg);
		orderStateRec.setId(System.currentTimeMillis() + "");
		omOrderStateRecDao.insert(orderStateRec);
	}

	/**
	 * 指派售后工单
	 * 
	 */
	@Transactional(readOnly = false)
	public String changServerWork(UserOrder order, String trackStaffUserLoginName) {
		List<OmOrderTrack> orderTracks = omOrderTrackDao.findByOrderCode(order.getOrderCode());
		if (!OrderEnums.OrderTypeEnum.serviceOnOrder.value().equals(order.getOrderType())
				&& !OrderEnums.OrderTypeEnum.serviceOrder.value().equals(order.getOrderType())) {
			return "不是售后订单不能指派售后";
		}
		OmOrderTrack serveromOrderTrack = null;
		for (OmOrderTrack omOrderTrack : orderTracks) {
			if (OrderEnums.WorkOrderTypeEnum.service.value().equals(omOrderTrack.getType())) {
				serveromOrderTrack = omOrderTrack;
			}
		}
		MainUser user = UserUtils.getByLoginName(trackStaffUserLoginName);
		if (user == null) {
			return "售后不存在";
		}
		if (!user.getUserType().equals("90W")) {
			return "不是售后账号";
		}
		if (serveromOrderTrack == null) {
			UserOrderItem item = new UserOrderItem();
			item.setOrderCode(order.getOrderCode());
			List<UserOrderItem> items = orderItemDao.findList(item);
			item=null;
			for (UserOrderItem userOrderItem : items) {
				if("2".equals(userOrderItem.getType())){
					item=userOrderItem;
				}
			}
			if(item==null){
				return "没有服务项";
			}
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			OmOrderTrack omOrderTrack = new OmOrderTrack();
			omOrderTrack.setId(format.format(new Date()));
			omOrderTrack.setStatus("1");
			omOrderTrack.setCreateDate(new Date());
			omOrderTrack.setOrderCode(order.getOrderCode());
			omOrderTrack.setUpdateDate(new Date());
			omOrderTrack.setType(OrderEnums.WorkOrderTypeEnum.service.value());
			omOrderTrack.setTrackStaffId(user.getId());
			omOrderTrack.setTrackComments("");
			omOrderTrack.setTrackStaffName(user.getLoginName());
			Map<String, String> param = new HashMap<>();
			param.put("workId", omOrderTrack.getId());
			param.put("orderItemCode", item.getOrderItemCode());
			orderTrackDao.insertWorkOrderItem(param);
			orderTrackDao.insert(omOrderTrack);
			item.setAssign(1);
			orderItemDao.update(item);
		} else {
			serveromOrderTrack.setTrackStaffId(user.getId());
			serveromOrderTrack.setTrackStaffName(user.getLoginName());
			orderTrackDao.update(serveromOrderTrack);
		}
		return "修改成功";

	}
}